/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2010
 */

package com.sisterandbrother.dao;

import static cn.org.rapid_framework.util.ObjectUtils.isNotEmpty;

import java.util.ArrayList;
import java.util.List;

import javacommon.base.BaseHibernateDao;

import org.springframework.stereotype.Repository;

import cn.org.rapid_framework.page.Page;

import com.sisterandbrother.model.ChatMsg;
import com.sisterandbrother.query.ChatMsgQuery;

@Repository
public class ChatMsgDao extends BaseHibernateDao<ChatMsg, Long> {

	@SuppressWarnings("rawtypes")
	public Class getEntityClass() {
		return ChatMsg.class;
	}

	@SuppressWarnings("rawtypes")
	public Page findPage(ChatMsgQuery query) {
		StringBuilder sql2 = new StringBuilder("select t from ChatMsg t where 1=1 ");
		if (isNotEmpty(query.getId())) {
			sql2.append(" and  t.id = :id ");
		}

		if (isNotEmpty(query.getSenderName())) {
			sql2.append(" and t.senderName = :senderName ");
		}
		if (isNotEmpty(query.getStartDate())) {
			sql2.append(" and t.senderTime >= :startDate ");
		}
		if (isNotEmpty(query.getContentSegment())) {
			sql2.append(" /~ and t.content like '%[:contentSegment]%' ~/");
		}

		if (isNotEmpty(query.getSortColumns())) {
			sql2.append(" order by  ").append(query.getSortColumns());
		}
		System.out.println(sql2.toString());
		return pageQuery(sql2.toString(), query);
	}

	public List<ChatMsg> findByQuery(ChatMsgQuery query) {
		return super.findAll("select t from ChatMsg t where 1=1 and t.id > " + query.getMinId());
	}

}
